# 参数：图片路径、数据格式List
# 约定数据格式
import matplotlib
import numpy as np
from matplotlib import pyplot as plt
Zh_Font = matplotlib.font_manager.FontProperties(fname="C:\\projectSoftware\\project\\python\\ZLY_GA_FJSP\\venv\\Include\\SourceHanSansSC-Bold.otf")
plt.rc('font', family='Times New Roman')
plt.rc('font', size=16)
plt.rc('font', weight='bold')
legend_font = {
    'fname': 'C:/Windows/fonts/simhei.ttf',  # 引用系统字体
    'style': 'normal',
    'size': 14,  # 字号
    'weight': "normal",  # 是否加粗，不加粗
}

def Data_NoDominate_Ship(String_File):
    lists = []
    key = String_File.split('::')[0]
    value = String_File.split('::')[1]
    # 掐头去尾，初步分隔,字符串：'[[38, 42], [18, 28]]' --> 字符串数组：['38, 42', '18, 28']
    value1 = value.replace('[[', '').replace(']]', '').split('], [')
    # 字符串数组在以', '分隔，后字符串转数字，添加到列表
    for str in value1:
        list = []
        for num in str.split(', '):
            list.append(int(num))
        lists.append(list)
    return lists
def Draw_Gatt_Me_Points_2d_sort(Population_Member, NoDominate_Ship, Image_Output_Path):
    # 数据准备
    X = []
    Y = []
    Nodominate_Ship_sort = NoDominate_Ship

    for adaptability in Population_Member:
        X.append(adaptability[0])
        Y.append(adaptability[1])
    print('X = ', X)
    print('Y = ', Y)

    # 画图
    plt.figure(figsize=(20, 10))
    plt.xticks(np.arange(0, max(X) + 5, 10), fontsize=18, fontproperties=Zh_Font)
    plt.yticks(np.arange(0, max(Y) + 5, 10), fontsize=18, fontproperties=Zh_Font)
    plt.title("甘特图", fontsize=30, fontproperties=Zh_Font)
    plt.ylabel('Critical Line Loads', fontsize=20, fontproperties=Zh_Font)
    plt.xlabel('Minimum completion time', fontsize=20, fontproperties=Zh_Font)
    Color = ['pink', 'tan', 'red', 'black','darkgreen', 'royalblue', 'lightcoral']
    Size  = [80, 90, 100, 110]
    color = ['pink']*len(X)
    size = [100]*len(X)
    print(color)
    for i in range(len(Nodominate_Ship_sort)):  # 同一组的个体
        for j in range(len(Nodominate_Ship_sort[i])):
            index = Nodominate_Ship_sort[i][j]
            color[index] = Color[i % len(Color)]
            size[index] = Size[i % len(Size)]
            if(j < len(Nodominate_Ship_sort[i])-1):
                # plt.plot(点坐标，点坐标，颜色)
                plt.plot([X[Nodominate_Ship_sort[i][j]],X[Nodominate_Ship_sort[i][j+1]]], [Y[Nodominate_Ship_sort[i][j]],Y[Nodominate_Ship_sort[i][j+1]]], color[index])
    print(color)
    for a, b in zip(X, Y):  # 添加这个循环显示坐标
        # plt.text（x值，y值，（坐标格式），位置，位置，大小）bottom
        plt.text(a, b, (a,b), ha='center', va='bottom', fontsize=13)
    plt.scatter(X, Y, c=color, s=size)
    # plt.show()
    plt.savefig(Image_Output_Path, dpi=300)


if __name__ == "__main__":
    file = "C:\\projectSoftware\\project\\zhilu\\zhilu-engine\\zhilu-common\\src\\main\\java\\com\\zhilu\\common\\ga\\file_look\\NoDominate_Ship_Image.txt"
    file_object = open(file, 'r')
    try:
        lines = file_object.readlines()
        for line in lines:
            print(line)
    finally:
        file_object.close()

    Population_Member = Data_NoDominate_Ship(lines[1])
    print('Population_Member = ', Population_Member)
    NoDominate_Ship = Data_NoDominate_Ship(lines[2])
    print('NoDominate_Ship = ', NoDominate_Ship)
    Image_Output_Path = lines[0].split('::')[1].replace('\\', '\\\\')
    Image_Output_Path = Image_Output_Path.replace('\n', '')
    print('Image_Output_Path = ', Image_Output_Path)
    Draw_Gatt_Me_Points_2d_sort(Population_Member, NoDominate_Ship, Image_Output_Path)
